Repetier-Firmware  1.0.0
HAL.h
Go to the documentation of this file.
1 /*
2  This file is part of Repetier-Firmware.
3 
4  Repetier-Firmware is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  Repetier-Firmware is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with Repetier-Firmware. If not, see <http://www.gnu.org/licenses/>.
16 
17  This firmware is a nearly complete rewrite of the sprinter firmware
18  by kliment (https://github.com/kliment/Sprinter)
19  which based on Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware.
20 
21  Functions in this file are used to communicate using ascii or repetier protocol.
22 */
23 
24 #ifndef HAL_H
25 #define HAL_H
26 
33 #include <avr/pgmspace.h>
34 #include <avr/io.h>
35 
36 
37 #define INLINE __attribute__((always_inline))
38 
39 #if CPU_ARCH == ARCH_AVR
40 #include <avr/io.h>
41 #else
42 #define PROGMEM
43 #define PGM_P const char *
44 #define PSTR(s) s
45 #define pgm_read_byte_near(x) (*(uint8_t*)x)
46 #define pgm_read_byte(x) (*(uint8_t*)x)
47 #endif
48 
49 #define PACK
50 
51 #define FSTRINGVALUE(var,value) const char var[] PROGMEM = value;
52 #define FSTRINGVAR(var) static const char var[] PROGMEM;
53 #define FSTRINGPARAM(var) PGM_P var
54 
55 #include <avr/eeprom.h>
56 #include <avr/wdt.h>
60 #define TIMER0_PRESCALE 64
61 
62 #define ANALOG_PRESCALER _BV(ADPS0)|_BV(ADPS1)|_BV(ADPS2)
63 
64 #if MOTHERBOARD==8 || MOTHERBOARD==88 || MOTHERBOARD==9 || MOTHERBOARD==92 || CPU_ARCH!=ARCH_AVR
65 #define EXTERNALSERIAL
66 #endif
67 #if NEW_COMMUNICATION && defined(BLUETOOTH_SERIAL) && BLUETOOTH_SERIAL > 0
68 #undef EXTERNALSERIAL
69 #define EXTERNALSERIAL
70 #endif
71 
72 //#define EXTERNALSERIAL // Force using Arduino serial
73 #ifndef EXTERNALSERIAL
74 #undef HardwareSerial_h
75 #define HardwareSerial_h // Don't use standard serial console
76 #endif
77 #include <inttypes.h>
78 #include "Stream.h"
79 #ifdef EXTERNALSERIAL
80 #define SERIAL_RX_BUFFER_SIZE 128
81 #endif
82 #if defined(ARDUINO) && ARDUINO >= 100
83 #include "Arduino.h"
84 #else
85 #include "WProgram.h"
86 #define COMPAT_PRE1
87 #endif
88 #if CPU_ARCH==ARCH_AVR
89 #include "fastio.h"
90 #else
91 #define READ(IO) digitalRead(IO)
92 #define WRITE(IO, v) digitalWrite(IO, v)
93 #define SET_INPUT(IO) pinMode(IO, INPUT)
94 #define SET_OUTPUT(IO) pinMode(IO, OUTPUT)
95 #endif
96 #ifndef cbi
97 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
98 #endif
99 #ifndef sbi
100 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
101 #endif
102 
104 {
105  uint8_t sreg;
106 public:
107  inline void protect()
108  {
109  cli();
110  }
111 
112  inline void unprotect()
113  {
114  SREG = sreg;
115  }
116 
117  inline InterruptProtectedBlock(bool later = false)
118  {
119  sreg = SREG;
120  if(!later)
121  cli();
122  }
123 
125  {
126  SREG = sreg;
127  }
128 };
129 
130 #define EEPROM_OFFSET 0
131 #define SECONDS_TO_TICKS(s) (unsigned long)(s*(float)F_CPU)
132 #define ANALOG_INPUT_SAMPLE 5
133 // Bits of the ADC converter
134 #define ANALOG_INPUT_BITS 10
135 #define ANALOG_REDUCE_BITS 0
136 #define ANALOG_REDUCE_FACTOR 1
137 
138 #define MAX_RAM 32767
139 
140 #define bit_clear(x,y) x&= ~(1<<y) //cbi(x,y)
141 #define bit_set(x,y) x|= (1<<y)//sbi(x,y)
142 
144 #define I2C_READ 1
145 
146 #define I2C_WRITE 0
147 
148 #if NONLINEAR_SYSTEM
149 // Maximum speed with 100% interrupt utilization is 27000 Hz at 16MHz cpu
150 // leave some margin for all the extra transformations. So we keep inside clean timings.
151 #define LIMIT_INTERVAL ((F_CPU/30000)+1)
152 #else
153 #define LIMIT_INTERVAL ((F_CPU/40000)+1)
154 #endif
155 
156 typedef uint16_t speed_t;
157 typedef uint32_t ticks_t;
158 typedef uint32_t millis_t;
159 typedef uint8_t flag8_t;
160 typedef int8_t fast8_t;
161 typedef uint8_t ufast8_t;
162 
163 #define FAST_INTEGER_SQRT
164 
165 #ifndef EXTERNALSERIAL
166 // Implement serial communication for one stream only!
167 /*
168  HardwareSerial.h - Hardware serial library for Wiring
169  Copyright (c) 2006 Nicholas Zambetti. All right reserved.
170 
171  This library is free software; you can redistribute it and/or
172  modify it under the terms of the GNU Lesser General Public
173  License as published by the Free Software Foundation; either
174  version 2.1 of the License, or (at your option) any later version.
175 
176  This library is distributed in the hope that it will be useful,
177  but WITHOUT ANY WARRANTY; without even the implied warranty of
178  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
179  Lesser General Public License for more details.
180 
181  You should have received a copy of the GNU Lesser General Public
182  License along with this library; if not, write to the Free Software
183  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
184 
185  Modified 28 September 2010 by Mark Sproul
186 
187  Modified to use only 1 queue with fixed length by Repetier
188 */
189 
190 #define SERIAL_BUFFER_SIZE 128
191 #define SERIAL_BUFFER_MASK 127
192 #undef SERIAL_TX_BUFFER_SIZE
193 #undef SERIAL_TX_BUFFER_MASK
194 #ifdef BIG_OUTPUT_BUFFER
195  #define SERIAL_TX_BUFFER_SIZE 128
196  #define SERIAL_TX_BUFFER_MASK 127
197 #else
198  #define SERIAL_TX_BUFFER_SIZE 64
199  #define SERIAL_TX_BUFFER_MASK 63
200 #endif
201 
203 {
204  uint8_t buffer[SERIAL_BUFFER_SIZE];
205  volatile uint8_t head;
206  volatile uint8_t tail;
207 };
209 {
210  uint8_t buffer[SERIAL_TX_BUFFER_SIZE];
211  volatile uint8_t head;
212  volatile uint8_t tail;
213 };
214 
215 class RFHardwareSerial : public Stream
216 {
217 public:
220  volatile uint8_t *_ubrrh;
221  volatile uint8_t *_ubrrl;
222  volatile uint8_t *_ucsra;
223  volatile uint8_t *_ucsrb;
224  volatile uint8_t *_udr;
225  uint8_t _rxen;
226  uint8_t _txen;
227  uint8_t _rxcie;
228  uint8_t _udrie;
229  uint8_t _u2x;
230 public:
231  RFHardwareSerial(ring_buffer *rx_buffer, ring_buffer_tx *tx_buffer,
232  volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
233  volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
234  volatile uint8_t *udr,
235  uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x);
236  void begin(unsigned long);
237  void end();
238  virtual int available(void);
239  virtual int peek(void);
240  virtual int read(void);
241  virtual void flush(void);
242 #ifdef COMPAT_PRE1
243  virtual void write(uint8_t);
244 #else
245  virtual size_t write(uint8_t);
246 #endif
247  using Print::write; // pull in write(str) and write(buf, size) from Print
248  operator bool();
249  int outputUnused(void); // Used for output in interrupts
250 };
252 #define RFSERIAL RFSerial
253 //extern ring_buffer tx_buffer;
254 #define WAIT_OUT_EMPTY while(tx_buffer.head != tx_buffer.tail) {}
255 #else
256 #define RFSERIAL Serial
257 #if defined(BLUETOOTH_SERIAL) && BLUETOOTH_SERIAL > 0
258 #if BLUETOOTH_SERIAL == 1
259 #define RFSERIAL2 Serial1
260 #elif BLUETOOTH_SERIAL == 2
261 #define RFSERIAL2 Serial2
262 #elif BLUETOOTH_SERIAL == 3
263 #define RFSERIAL2 Serial3
264 #elif BLUETOOTH_SERIAL == 4
265 #define RFSERIAL2 Serial4
266 #elif BLUETOOTH_SERIAL == 5
267 #define RFSERIAL2 Serial5
268 #endif
269 #endif
270 #endif
271 
272 class HAL
273 {
274 public:
275 #if FEATURE_WATCHDOG
276  static bool wdPinged;
277 #endif
278  HAL();
279  virtual ~HAL();
280  static inline void hwSetup(void)
281  {}
282  // return val*val
283  static uint16_t integerSqrt(uint32_t a);
291  static inline int32_t Div4U2U(uint32_t a,uint16_t b)
292  {
293 #if CPU_ARCH==ARCH_AVR
294  // r14/r15 remainder
295  // r16 counter
296  __asm__ __volatile__ (
297  "clr r14 \n\t"
298  "sub r15,r15 \n\t"
299  "tst %D0 \n\t"
300  "brne do32%= \n\t"
301  "tst %C0 \n\t"
302  "breq donot24%= \n\t"
303  "rjmp do24%= \n\t"
304  "donot24%=:" "ldi r16,17 \n\t" // 16 Bit divide
305  "d16u_1%=:" "rol %A0 \n\t"
306  "rol %B0 \n\t"
307  "dec r16 \n\t"
308  "brne d16u_2%= \n\t"
309  "rjmp end%= \n\t"
310  "d16u_2%=:" "rol r14 \n\t"
311  "rol r15 \n\t"
312  "sub r14,%A2 \n\t"
313  "sbc r15,%B2 \n\t"
314  "brcc d16u_3%= \n\t"
315  "add r14,%A2 \n\t"
316  "adc r15,%B2 \n\t"
317  "clc \n\t"
318  "rjmp d16u_1%= \n\t"
319  "d16u_3%=:" "sec \n\t"
320  "rjmp d16u_1%= \n\t"
321  "do32%=:" // divide full 32 bit
322  "rjmp do32B%= \n\t"
323  "do24%=:" // divide 24 bit
324 
325  "ldi r16,25 \n\t" // 24 Bit divide
326  "d24u_1%=:" "rol %A0 \n\t"
327  "rol %B0 \n\t"
328  "rol %C0 \n\t"
329  "dec r16 \n\t"
330  "brne d24u_2%= \n\t"
331  "rjmp end%= \n\t"
332  "d24u_2%=:" "rol r14 \n\t"
333  "rol r15 \n\t"
334  "sub r14,%A2 \n\t"
335  "sbc r15,%B2 \n\t"
336  "brcc d24u_3%= \n\t"
337  "add r14,%A2 \n\t"
338  "adc r15,%B2 \n\t"
339  "clc \n\t"
340  "rjmp d24u_1%= \n\t"
341  "d24u_3%=:" "sec \n\t"
342  "rjmp d24u_1%= \n\t"
343 
344  "do32B%=:" // divide full 32 bit
345 
346  "ldi r16,33 \n\t" // 32 Bit divide
347  "d32u_1%=:" "rol %A0 \n\t"
348  "rol %B0 \n\t"
349  "rol %C0 \n\t"
350  "rol %D0 \n\t"
351  "dec r16 \n\t"
352  "brne d32u_2%= \n\t"
353  "rjmp end%= \n\t"
354  "d32u_2%=:" "rol r14 \n\t"
355  "rol r15 \n\t"
356  "sub r14,%A2 \n\t"
357  "sbc r15,%B2 \n\t"
358  "brcc d32u_3%= \n\t"
359  "add r14,%A2 \n\t"
360  "adc r15,%B2 \n\t"
361  "clc \n\t"
362  "rjmp d32u_1%= \n\t"
363  "d32u_3%=:" "sec \n\t"
364  "rjmp d32u_1%= \n\t"
365 
366  "end%=:" // end
367  :"=&r"(a)
368  :"0"(a),"r"(b)
369  :"r14","r15","r16"
370  );
371  return a;
372 #else
373  return a/b;
374 #endif
375  }
376  static inline unsigned long U16SquaredToU32(unsigned int val)
377  {
378  long res;
379  __asm__ __volatile__ ( // 15 Ticks
380  "mul %A1,%A1 \n\t"
381  "movw %A0,r0 \n\t"
382  "mul %B1,%B1 \n\t"
383  "movw %C0,r0 \n\t"
384  "mul %A1,%B1 \n\t"
385  "clr %A1 \n\t"
386  "add %B0,r0 \n\t"
387  "adc %C0,r1 \n\t"
388  "adc %D0,%A1 \n\t"
389  "add %B0,r0 \n\t"
390  "adc %C0,r1 \n\t"
391  "adc %D0,%A1 \n\t"
392  "clr r1 \n\t"
393  : "=&r"(res),"=r"(val)
394  : "1"(val)
395  );
396  return res;
397  }
398  static inline unsigned int ComputeV(long timer,long accel)
399  {
400 #if CPU_ARCH==ARCH_AVR
401  unsigned int res;
402  // 38 Ticks
403  __asm__ __volatile__ ( // 0 = res, 1 = timer, 2 = accel %D2=0 ,%A1 are unused is free
404  // Result LSB first: %A0, %B0, %A1
405  "mul %B1,%A2 \n\t"
406  "mov %A0,r1 \n\t"
407  "mul %B1,%C2 \n\t"
408  "mov %B0,r0 \n\t"
409  "mov %A1,r1 \n\t"
410  "mul %B1,%B2 \n\t"
411  "add %A0,r0 \n\t"
412  "adc %B0,r1 \n\t"
413  "adc %A1,%D2 \n\t"
414  "mul %C1,%A2 \n\t"
415  "add %A0,r0 \n\t"
416  "adc %B0,r1 \n\t"
417  "adc %A1,%D2 \n\t"
418  "mul %C1,%B2 \n\t"
419  "add %B0,r0 \n\t"
420  "adc %A1,r1 \n\t"
421  "mul %D1,%A2 \n\t"
422  "add %B0,r0 \n\t"
423  "adc %A1,r1 \n\t"
424  "mul %C1,%C2 \n\t"
425  "add %A1,r0 \n\t"
426  "mul %D1,%B2 \n\t"
427  "add %A1,r0 \n\t"
428  "lsr %A1 \n\t"
429  "ror %B0 \n\t"
430  "ror %A0 \n\t"
431  "lsr %A1 \n\t"
432  "ror %B0 \n\t"
433  "ror %A0 \n\t"
434  "clr r1 \n\t"
435  :"=&r"(res),"=r"(timer),"=r"(accel)
436  :"1"(timer),"2"(accel)
437  : );
438  // unsigned int v = ((timer>>8)*cur->accel)>>10;
439  return res;
440 #else
441  return ((timer >> 8) * accel) >> 10;
442 #endif
443  }
444 // Multiply two 16 bit values and return 32 bit result
445  static inline uint32_t mulu16xu16to32(unsigned int a,unsigned int b)
446  {
447  uint32_t res;
448  // 18 Ticks = 1.125 us
449  __asm__ __volatile__ ( // 0 = res, 1 = timer, 2 = accel %D2=0 ,%A1 are unused is free
450  // Result LSB first: %A0, %B0, %A1
451  "clr r18 \n\t"
452  "mul %B2,%B1 \n\t" // mul hig bytes
453  "movw %C0,r0 \n\t"
454  "mul %A1,%A2 \n\t" // mul low bytes
455  "movw %A0,r0 \n\t"
456  "mul %A1,%B2 \n\t"
457  "add %B0,r0 \n\t"
458  "adc %C0,r1 \n\t"
459  "adc %D0,r18 \n\t"
460  "mul %B1,%A2 \n\t"
461  "add %B0,r0 \n\t"
462  "adc %C0,r1 \n\t"
463  "adc %D0,r18 \n\t"
464  "clr r1 \n\t"
465  :"=&r"(res),"=r"(a),"=r"(b)
466  :"1"(a),"2"(b)
467  :"r18" );
468  // return (long)a*b;
469  return res;
470  }
471 // Multiply two 16 bit values and return 32 bit result
472  static inline unsigned int mulu6xu16shift16(unsigned int a,unsigned int b)
473  {
474 #if CPU_ARCH == ARCH_AVR
475  unsigned int res;
476  // 18 Ticks = 1.125 us
477  __asm__ __volatile__ ( // 0 = res, 1 = timer, 2 = accel %D2=0 ,%A1 are unused is free
478  // Result LSB first: %A0, %B0, %A1
479  "clr r18 \n\t"
480  "mul %B2,%B1 \n\t" // mul hig bytes
481  "movw %A0,r0 \n\t"
482  "mul %A1,%A2 \n\t" // mul low bytes
483  "mov r19,r1 \n\t"
484  "mul %A1,%B2 \n\t"
485  "add r19,r0 \n\t"
486  "adc %A0,r1 \n\t"
487  "adc %B0,r18 \n\t"
488  "mul %B1,%A2 \n\t"
489  "add r19,r0 \n\t"
490  "adc %A0,r1 \n\t"
491  "adc %B0,r18 \n\t"
492  "clr r1 \n\t"
493  :"=&r"(res),"=r"(a),"=r"(b)
494  :"1"(a),"2"(b)
495  :"r18","r19" );
496  return res;
497 #else
498  return ((int32_t)a * b) >> 16;
499 #endif
500  }
501  static inline void digitalWrite(uint8_t pin,uint8_t value)
502  {
503  ::digitalWrite(pin,value);
504  }
505  static inline uint8_t digitalRead(uint8_t pin)
506  {
507  return ::digitalRead(pin);
508  }
509  static inline void pinMode(uint8_t pin,uint8_t mode)
510  {
511  ::pinMode(pin,mode);
512  }
513  static int32_t CPUDivU2(unsigned int divisor);
514  static inline void delayMicroseconds(unsigned int delayUs)
515  {
516  ::delayMicroseconds(delayUs);
517  }
518  static inline void delayMilliseconds(unsigned int delayMs)
519  {
520  ::delay(delayMs);
521  }
522  static inline void tone(uint8_t pin,int duration)
523  {
524  ::tone(pin,duration);
525  }
526  static inline void noTone(uint8_t pin)
527  {
528  ::noTone(pin);
529  }
530  static inline void eprSetByte(unsigned int pos,uint8_t value)
531  {
532  eeprom_write_byte((unsigned char *)(EEPROM_OFFSET + pos), value);
533  }
534  static inline void eprSetInt16(unsigned int pos,int16_t value)
535  {
536  eeprom_write_word((unsigned int*)(EEPROM_OFFSET + pos),value);
537  }
538  static inline void eprSetInt32(unsigned int pos,int32_t value)
539  {
540  eeprom_write_dword((uint32_t*)(EEPROM_OFFSET + pos),value);
541  }
542  static inline void eprSetFloat(unsigned int pos,float value)
543  {
544  eeprom_write_block(&value,(void*)(EEPROM_OFFSET + pos), 4);
545  }
546  static inline uint8_t eprGetByte(unsigned int pos)
547  {
548  return eeprom_read_byte ((unsigned char *)(EEPROM_OFFSET + pos));
549  }
550  static inline int16_t eprGetInt16(unsigned int pos)
551  {
552  return eeprom_read_word((uint16_t *)(EEPROM_OFFSET + pos));
553  }
554  static inline int32_t eprGetInt32(unsigned int pos)
555  {
556  return eeprom_read_dword((uint32_t*)(EEPROM_OFFSET + pos));
557  }
558  static inline float eprGetFloat(unsigned int pos)
559  {
560  float v;
561  eeprom_read_block(&v,(void *)(EEPROM_OFFSET + pos),4); // newer gcc have eeprom_read_block but not arduino 22
562  return v;
563  }
564 
565  // Faster version of InterruptProtectedBlock.
566  // For safety it ma yonly be called from within an
567  // interrupt handler.
568  static inline void allowInterrupts()
569  {
570  sei();
571  }
572 
573  // Faster version of InterruptProtectedBlock.
574  // For safety it may only be called from within an
575  // interrupt handler.
576  static inline void forbidInterrupts()
577  {
578  cli();
579  }
580  static inline millis_t timeInMilliseconds()
581  {
582  return millis();
583  }
584  static inline char readFlashByte(PGM_P ptr)
585  {
586  return pgm_read_byte(ptr);
587  }
588  static inline int16_t readFlashWord(PGM_P ptr)
589  {
590  return pgm_read_word(ptr);
591  }
592  static inline void serialSetBaudrate(long baud)
593  {
594  RFSERIAL.begin(baud);
595  }
596  static inline bool serialByteAvailable()
597  {
598  return RFSERIAL.available() > 0;
599  }
600  static inline uint8_t serialReadByte()
601  {
602  return RFSERIAL.read();
603  }
604  static inline void serialWriteByte(char b)
605  {
606  RFSERIAL.write(b);
607  }
608  static inline void serialFlush()
609  {
610  RFSERIAL.flush();
611  }
612  static void setupTimer();
613  static void showStartReason();
614  static int getFreeRam();
615  static void resetHardware();
616 
617  // SPI related functions
618  static void spiBegin()
619  {
620 #if SDSS >= 0
621  SET_INPUT(MISO_PIN);
622  SET_OUTPUT(MOSI_PIN);
623  SET_OUTPUT(SCK_PIN);
624  // SS must be in output mode even it is not chip select
625  SET_OUTPUT(SDSS);
626 #if SDSSORIG >- 1
628 #endif
629  // set SS high - may be chip select for another SPI device
630 #if defined(SET_SPI_SS_HIGH) && SET_SPI_SS_HIGH
631  WRITE(SDSS, HIGH);
632 #endif // SET_SPI_SS_HIGH
633 #endif
634  }
635  static inline void spiInit(uint8_t spiRate)
636  {
637  uint8_t r = 0;
638  for (uint8_t b = 2; spiRate > b && r < 6; b <<= 1, r++);
639 
640  SET_OUTPUT(SS);
641  WRITE(SS,HIGH);
642  SET_OUTPUT(SCK);
643  SET_OUTPUT(MOSI_PIN);
644  SET_INPUT(MISO_PIN);
645 #ifdef PRR
646  PRR &= ~(1<<PRSPI);
647 #elif defined PRR0
648  PRR0 &= ~(1<<PRSPI);
649 #endif
650  // See avr processor documentation
651  SPCR = (1 << SPE) | (1 << MSTR) | (r >> 1);
652  SPSR = (r & 1 || r == 6 ? 0 : 1) << SPI2X;
653 
654  }
655  static inline uint8_t spiReceive(uint8_t send=0xff)
656  {
657  SPDR = send;
658  while (!(SPSR & (1 << SPIF))) {}
659  return SPDR;
660  }
661  static inline void spiReadBlock(uint8_t*buf,size_t nbyte)
662  {
663  if (nbyte-- == 0) return;
664  SPDR = 0XFF;
665  for (size_t i = 0; i < nbyte; i++)
666  {
667  while (!(SPSR & (1 << SPIF))) {}
668  buf[i] = SPDR;
669  SPDR = 0XFF;
670  }
671  while (!(SPSR & (1 << SPIF))) {}
672  buf[nbyte] = SPDR;
673  }
674  static inline void spiSend(uint8_t b)
675  {
676  SPDR = b;
677  while (!(SPSR & (1 << SPIF))) {}
678  }
679  static inline void spiSend(const uint8_t* buf , size_t n)
680  {
681  if (n == 0) return;
682  SPDR = buf[0];
683  if (n > 1)
684  {
685  uint8_t b = buf[1];
686  size_t i = 2;
687  while (1)
688  {
689  while (!(SPSR & (1 << SPIF))) {}
690  SPDR = b;
691  if (i == n) break;
692  b = buf[i++];
693  }
694  }
695  while (!(SPSR & (1 << SPIF))) {}
696  }
697 
698  static inline __attribute__((always_inline))
699  void spiSendBlock(uint8_t token, const uint8_t* buf)
700  {
701  SPDR = token;
702  for (uint16_t i = 0; i < 512; i += 2)
703  {
704  while (!(SPSR & (1 << SPIF))) {}
705  SPDR = buf[i];
706  while (!(SPSR & (1 << SPIF))) {}
707  SPDR = buf[i + 1];
708  }
709  while (!(SPSR & (1 << SPIF))) {}
710  }
711 
712  // I2C Support
713 
714  static void i2cSetClockspeed(uint32_t clockSpeedHz);
715  static void i2cInit(uint32_t clockSpeedHz);
716  static unsigned char i2cStart(uint8_t address);
717  static void i2cStartWait(uint8_t address);
718  static void i2cStop(void);
719  static void i2cWrite( uint8_t data );
720  static uint8_t i2cReadAck(void);
721  static uint8_t i2cReadNak(void);
722 
723  // Watchdog support
724 
725  inline static void startWatchdog()
726  {
727 #if defined (__AVR_ATmega1280__) || defined (__AVR_ATmega2560__)
728  WDTCSR = (1<<WDCE) | (1<<WDE); // wdt FIX for arduino mega boards
729  WDTCSR = (1<<WDIE) | (1<<WDP3);
730 #else
731  wdt_enable(WDTO_4S);
732 #endif
733  };
734  inline static void stopWatchdog()
735  {
736  wdt_disable();
737  }
738  inline static void pingWatchdog()
739  {
740 #if FEATURE_WATCHDOG
741  wdPinged = true;
742 #endif
743  };
744  inline static float maxExtruderTimerFrequency()
745  {
746  return (float)F_CPU/TIMER0_PRESCALE;
747  }
748 #if FEATURE_SERVO
749  static unsigned int servoTimings[4];
750  static void servoMicroseconds(uint8_t servo,int ms, uint16_t autoOff);
751 #endif
752  static void analogStart();
753 #if USE_ADVANCE
754  static void resetExtruderDirection();
755 #endif
756 protected:
757 private:
758 };
759 /*#if MOTHERBOARD==6 || MOTHERBOARD==62 || MOTHERBOARD==7
760 #if MOTHERBOARD!=7
761 #define SIMULATE_PWM
762 #endif
763 #define EXTRUDER_TIMER_VECTOR TIMER2_COMPA_vect
764 #define EXTRUDER_OCR OCR2A
765 #define EXTRUDER_TCCR TCCR2A
766 #define EXTRUDER_TIMSK TIMSK2
767 #define EXTRUDER_OCIE OCIE2A
768 #define PWM_TIMER_VECTOR TIMER2_COMPB_vect
769 #define PWM_OCR OCR2B
770 #define PWM_TCCR TCCR2B
771 #define PWM_TIMSK TIMSK2
772 #define PWM_OCIE OCIE2B
773 #else*/
774 #define EXTRUDER_TIMER_VECTOR TIMER0_COMPA_vect
775 #define EXTRUDER_OCR OCR0A
776 #define EXTRUDER_TCCR TCCR0A
777 #define EXTRUDER_TIMSK TIMSK0
778 #define EXTRUDER_OCIE OCIE0A
779 #define PWM_TIMER_VECTOR TIMER0_COMPB_vect
780 #define PWM_OCR OCR0B
781 #define PWM_TCCR TCCR0A
782 #define PWM_TIMSK TIMSK0
783 #define PWM_OCIE OCIE0B
784 //#endif
785 #endif // HAL_H
static int16_t eprGetInt16(unsigned int pos)
Definition: HAL.h:550
static float maxExtruderTimerFrequency()
Definition: HAL.h:744
static uint8_t spiReceive(uint8_t send=0xff)
Definition: HAL.h:655
#define SERIAL_TX_BUFFER_SIZE
Definition: HAL.h:198
static void allowInterrupts()
Definition: HAL.h:568
static float eprGetFloat(unsigned int pos)
Definition: HAL.h:558
static void digitalWrite(uint8_t pin, uint8_t value)
Definition: HAL.h:501
uint8_t _txen
Definition: HAL.h:226
volatile uint8_t tail
Definition: HAL.h:206
static millis_t timeInMilliseconds()
Definition: HAL.h:580
static uint32_t mulu16xu16to32(unsigned int a, unsigned int b)
Definition: HAL.h:445
static uint8_t serialReadByte()
Definition: HAL.h:600
uint8_t _udrie
Definition: HAL.h:228
uint8_t _u2x
Definition: HAL.h:229
static void eprSetFloat(unsigned int pos, float value)
Definition: HAL.h:542
uint8_t flag8_t
Definition: HAL.h:159
static unsigned int mulu6xu16shift16(unsigned int a, unsigned int b)
Definition: HAL.h:472
static void delayMilliseconds(unsigned int delayMs)
Definition: HAL.h:518
void unprotect()
Definition: HAL.h:112
volatile uint8_t * _ubrrl
Definition: HAL.h:221
uint16_t speed_t
Definition: HAL.h:156
#define WRITE(IO, v)
Write to a pin wrapper.
Definition: fastio.h:48
static void spiSend(uint8_t b)
Definition: HAL.h:674
static void serialSetBaudrate(long baud)
Definition: HAL.h:592
Definition: HAL.h:272
static void pingWatchdog()
Definition: HAL.h:738
Definition: HAL.h:208
#define SDSSORIG
Definition: pins.h:3022
ring_buffer_tx * _tx_buffer
Definition: HAL.h:219
static unsigned long U16SquaredToU32(unsigned int val)
Definition: HAL.h:376
static char readFlashByte(PGM_P ptr)
Definition: HAL.h:584
static void startWatchdog()
Definition: HAL.h:725
volatile uint8_t * _ucsra
Definition: HAL.h:222
Definition: HAL.h:103
static uint8_t digitalRead(uint8_t pin)
Definition: HAL.h:505
#define SET_OUTPUT(IO)
set pin as output wrapper
Definition: fastio.h:56
static uint8_t eprGetByte(unsigned int pos)
Definition: HAL.h:546
static void hwSetup(void)
Definition: HAL.h:280
static int32_t eprGetInt32(unsigned int pos)
Definition: HAL.h:554
static void spiSend(const uint8_t *buf, size_t n)
Definition: HAL.h:679
static void spiInit(uint8_t spiRate)
Definition: HAL.h:635
static void serialWriteByte(char b)
Definition: HAL.h:604
#define EEPROM_OFFSET
Definition: HAL.h:130
uint8_t ufast8_t
Definition: HAL.h:161
volatile uint8_t * _ubrrh
Definition: HAL.h:220
static int32_t Div4U2U(uint32_t a, uint16_t b)
Optimized division.
Definition: HAL.h:291
#define SERIAL_BUFFER_SIZE
Definition: HAL.h:190
ring_buffer * _rx_buffer
Definition: HAL.h:218
static int16_t readFlashWord(PGM_P ptr)
Definition: HAL.h:588
Definition: HAL.h:215
RFHardwareSerial RFSerial
uint32_t millis_t
Definition: HAL.h:158
volatile uint8_t * _udr
Definition: HAL.h:224
static void eprSetInt32(unsigned int pos, int32_t value)
Definition: HAL.h:538
uint8_t sreg
Definition: HAL.h:105
static void eprSetByte(unsigned int pos, uint8_t value)
Definition: HAL.h:530
#define TIMER0_PRESCALE
Prescale factor, timer0 runs at.
Definition: HAL.h:60
#define SDSS
Definition: DisplayList.h:1245
volatile uint8_t * _ucsrb
Definition: HAL.h:223
~InterruptProtectedBlock()
Definition: HAL.h:124
static void forbidInterrupts()
Definition: HAL.h:576
static void tone(uint8_t pin, int duration)
Definition: HAL.h:522
static unsigned int ComputeV(long timer, long accel)
Definition: HAL.h:398
static void eprSetInt16(unsigned int pos, int16_t value)
Definition: HAL.h:534
void protect()
Definition: HAL.h:107
uint8_t _rxen
Definition: HAL.h:225
static bool serialByteAvailable()
Definition: HAL.h:596
volatile uint8_t tail
Definition: HAL.h:212
int8_t fast8_t
Definition: HAL.h:160
long __attribute__((used)) stepperWait=0
volatile uint8_t head
Definition: HAL.h:211
InterruptProtectedBlock(bool later=false)
Definition: HAL.h:117
uint32_t ticks_t
Definition: HAL.h:157
static void spiReadBlock(uint8_t *buf, size_t nbyte)
Definition: HAL.h:661
static void delayMicroseconds(unsigned int delayUs)
Definition: HAL.h:514
Definition: HAL.h:202
static void spiBegin()
Definition: HAL.h:618
static void noTone(uint8_t pin)
Definition: HAL.h:526
static void serialFlush()
Definition: HAL.h:608
uint8_t _rxcie
Definition: HAL.h:227
volatile uint8_t head
Definition: HAL.h:205
#define SET_INPUT(IO)
set pin as input wrapper
Definition: fastio.h:54
static void stopWatchdog()
Definition: HAL.h:734
#define RFSERIAL
Definition: HAL.h:252
static void pinMode(uint8_t pin, uint8_t mode)
Definition: HAL.h:509